/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.xabnf;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.util.IResourceScopeCache;

import com.google.inject.Inject;
import com.google.inject.Provider;

import de.thwildau.tm.moses.common.util.ResourcesUtil;
import de.thwildau.tm.moses.xabnf.xabnf.Xabnf;
 

/**
 * Helper class to find EMF resources.
 *
 * @author Thomas Kistel
 * @version $Revision: 762 $
 *
 */
public class ResourceFinder {
    
    private static Logger LOG = Logger.getLogger(ResourceFinder.class);
    
    @Inject
    private IResourceScopeCache cache;
    
    @Inject
    private ResourcesUtil resourcesUtil;
    
    public URI findAbnfUri(final EObject xabnfObject) {
        String file = rootOf(xabnfObject).getGeneratorModel().getFile();
        try {
            URI targetUri = resourcesUtil.findResource(xabnfObject.eResource().getURI(), file);
            return targetUri;
        } catch (CoreException e) {
            String message = "Cannot find target file " + file + " for EObject " + xabnfObject;
            LOG.error(message, e);
            return null;
        }
    }
    
    /**
     * Finds the related ABNF-resource of the given Xabnf-element. The abnf-file is specified in the
     * GeneratorModel of each xabnf-file. Returns <code>null</code> is the abnf resource cannot be found.
     * 
     * @param xabnfObject a child Xabnf object
     * @return the abnf resource object or <code>null</code>
     */
    public Resource findAbnfResource(final EObject xabnfObject) {
        final URI targetUri = findAbnfUri(xabnfObject);
        final Resource abnfResource = xabnfObject.eResource();
        if(targetUri != null) {
            return cache.get(xabnfObject, abnfResource, new Provider<Resource>() {

                @Override
                public Resource get() {
                    ResourceSet resourceSet = abnfResource.getResourceSet();
                    Resource resource = resourceSet.getResource(targetUri, true);
                    return resource;
                }
            });
        } else {
            cache.clear(abnfResource);
            return null;
        }
        
    }
    
    /**
     * Returns the root element of the Xabnf grammer.
     * 
     * @param xabnfObject a child Xabnf object whose root element is searched
     * @return the root {@link Xabnf} object
     * @throws IllegalArgumentException if the given obj is not part of Xabnf
     */
    public Xabnf rootOf(EObject xabnfObject) {
        EObject current = xabnfObject;
        while (!(current instanceof Xabnf)) {
            if(current == null) {
                throw new IllegalArgumentException("The object " + xabnfObject.eClass().getName() + " is not a XAbnf object.");
            }
            current = current.eContainer();
        }   
        return (Xabnf)current;
    }
}
